package com.clkj.zhonghui.controller.points;

import com.clkj.zhonghui.pojo.common.Result;
import com.clkj.zhonghui.pojo.common.StatusCode;
import com.clkj.zhonghui.pojo.points.RebatePoints;
import com.clkj.zhonghui.pojo.points.RebateRelations;
import com.clkj.zhonghui.pojo.points.RebateSettings;
import com.clkj.zhonghui.service.points.PointsService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Author 周佳润
 * @Date 2024/9/4 上午10:20
 */
@RestController
@RequestMapping(value = "/points")
@Api(value = "/points", tags = {"积分管理"})
@CrossOrigin
public class PointsController {

    @Autowired
    private PointsService pointsService;

    @PostMapping("/listPoints")
    @ApiOperation(value = "小程序--获取个人积分及详情记录")
    public Result listPoints(@ApiParam(name="userId",value = "用户ID")@RequestParam Integer userId){
        try {
            Map map =  pointsService.listPoints(userId);
            return new Result(true, StatusCode.OK,"查询成功",map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }


    @PostMapping("/listLowerUser")
    @ApiOperation(value = "获取用户下级")
    public Result listLowerUser(@ApiParam(name="userId",value = "用户ID")@RequestParam Integer userId){
        try {
            Map map =  pointsService.listLowerUser(userId);
            return new Result(true, StatusCode.OK,"查询成功",map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }
    @PostMapping("/listHigherUser")
    @ApiOperation(value = "获取用户上级")
    public Result listHigherUser(@ApiParam(name="userId",value = "用户ID")@RequestParam Integer userId){
        try {
            Map map =  pointsService.listHigherUser(userId);
            return new Result(true, StatusCode.OK,"查询成功",map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }
    @PostMapping("/editSetting")
    @ApiOperation(value = "后台---变更积分返利比例")
    public Result editSetting(RebateSettings rebateSettings){
        try {
            pointsService.editSetting(rebateSettings);
            return new Result(true, StatusCode.OK,"操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"操作失败");
        }
    }

    @PostMapping("/listSetting")
    @ApiOperation(value = "获取积分返利比例")
    public Result listSetting(){
        try {
            RebateSettings rebateSettings =  pointsService.listSetting();
            return new Result(true, StatusCode.OK,"查询成功",rebateSettings);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }

    @PostMapping("/increasePoints")
    @ApiOperation(value = "小程序----积分转增")
    public Result increasePoints( @ApiParam(name = "senderId", value = "发送人ID") @RequestParam("senderId") Integer senderId,
                                  @ApiParam(name = "receiverId", value = "接收人ID") @RequestParam("receiverId") Integer receiverId,
                                  @ApiParam(name = "point", value = "积分数") @RequestParam("point") Double point){
        try {
            pointsService.increasePoints(senderId,receiverId,point);
            return new Result(true, StatusCode.OK,"操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"操作失败");
        }
    }

    @PostMapping("/test")
    @ApiOperation(value = "测试测试测试测试")
    public Result test(){
        try {
            List<RebatePoints> rebatePoints = pointsService.test();
            return new Result(true, StatusCode.OK,"操作成功",rebatePoints);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"操作失败");
        }
    }

}
